import React from 'react';
import { BrowserRouter, Switch, Route, Redirect } from 'react-router-dom';
import { commonRouter, authRouter } from './router/index';

class App extends React.Component {
    render() {
        return (
            <BrowserRouter>
                <Switch>
                    <Redirect exact from="/" to="/blogs" />
                    {/* 不需要验证登录的路由 */}
                    {commonRouter.map((item, index) => {
                        return (
                            <Route
                                key={index}
                                path={item.path}
                                component={item.commponent}
                            />
                        );
                    })}
                    {/* 需要登录的路由 */}
                    {authRouter.map((item, index) => {
                        return (
                            <Route
                                key={index}
                                path={item.path}
                                render={(props) => {
                                    let token = window.sessionStorage.getItem(
                                        'token',
                                    );
                                    if (token) {
                                        return <item.commponent {...props} />;
                                    }
                                    return <Redirect to="/login" />;
                                }}
                            />
                        );
                    })}
                </Switch>
            </BrowserRouter>
        );
    }
}

export default App;
